/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkl53.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import pkl53.controller.exceptions.NonexistentEntityException;
import pkl53.controller.exceptions.PreexistingEntityException;
import pkl53.model.Blok5678coba;

/**
 *
 * @author Prima
 */
public class Blok5678cobaJpaController implements Serializable {

    public Blok5678cobaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Blok5678coba blok5678coba) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(blok5678coba);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findBlok5678coba(blok5678coba.getB5r1()) != null) {
                throw new PreexistingEntityException("Blok5678coba " + blok5678coba + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Blok5678coba blok5678coba) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            blok5678coba = em.merge(blok5678coba);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = blok5678coba.getB5r1();
                if (findBlok5678coba(id) == null) {
                    throw new NonexistentEntityException("The blok5678coba with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Blok5678coba blok5678coba;
            try {
                blok5678coba = em.getReference(Blok5678coba.class, id);
                blok5678coba.getB5r1();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The blok5678coba with id " + id + " no longer exists.", enfe);
            }
            em.remove(blok5678coba);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Blok5678coba> findBlok5678cobaEntities() {
        return findBlok5678cobaEntities(true, -1, -1);
    }

    public List<Blok5678coba> findBlok5678cobaEntities(int maxResults, int firstResult) {
        return findBlok5678cobaEntities(false, maxResults, firstResult);
    }

    private List<Blok5678coba> findBlok5678cobaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Blok5678coba.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Blok5678coba findBlok5678coba(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Blok5678coba.class, id);
        } finally {
            em.close();
        }
    }

    public int getBlok5678cobaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Blok5678coba> rt = cq.from(Blok5678coba.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
